#include "gtest/gtest.h"
#include "String.h"
#include <iostream>
using namespace std;

using namespace SUNSHINE;

class StringTest : public testing::Test
{
	protected:
		string s;
		vector<string> v;

		virtual void SetUp()
		{
			v.push_back("wqz");
			v.push_back("lgy");
			v.push_back("zhy");
			v.push_back("hhy");
			s.append("wqz&lgy&zhy&hhy");
		}

		virtual void TearDown()
		{
		}
};

TEST_F(StringTest, ConcatenateIfEmptyVector)
{
	v.clear();
	EXPECT_STREQ("\0", String::Concatenate(v, " ").c_str());
}

TEST_F(StringTest, ConcatenateIfSeparatorIsZero)
{
	EXPECT_STREQ("wqzlgyzhyhhy", String::Concatenate(v, "\0").c_str());
	EXPECT_STREQ("wqzlgyzhyhhy", String::Concatenate(v, "").c_str());
}

TEST_F(StringTest, ConcatenateNormal)
{
	EXPECT_STREQ("wqz|&|lgy|&|zhy|&|hhy",
			String::Concatenate(v, "|&|").c_str());
}

TEST_F(StringTest, SplitIfEmptyString)
{
	v.clear();
	s.clear();
	EXPECT_TRUE(v == String::Split(s, "separator"));
}

TEST_F(StringTest, SplitIfEmptySeparator)
{
	s.append("love & hate");
	v.clear();
	v.push_back(s);
	EXPECT_TRUE(v == String::Split(s, ""));
}

TEST_F(StringTest, SplitNormal)
{
	vector<string> actual;
	actual = String::Split(s, "&");

	EXPECT_TRUE(v == actual);
}

TEST_F(StringTest, SplitIfEndOfSeparator)
{
	v.push_back("");
	s.append("&");
	EXPECT_TRUE(v == String::Split(s, "&"));
}

TEST_F(StringTest, SplitIfHeaderWithSeparator)
{
	v.insert(v.begin(), "");
	s.insert(s.begin(), '&');
	EXPECT_TRUE(v == String::Split(s, "&"));
}

TEST_F(StringTest, SplitIfHeaderWithSeparatorAndEndOfSeparator)
{
	v.insert(v.begin(), "");
	v.push_back("");
	s.insert(s.begin(), '&');
	s.insert(s.end(), '&');
	EXPECT_TRUE(v == String::Split(s, "&"));
}

TEST_F(StringTest, SplitIfAllElementsAreEmpty)
{
	s.clear();
	int elementsCount = 4;
	for (int i = 0; i < elementsCount; ++i)
	{
		s.append("&");
	}
	
	v.clear();

	for (int i = 0; i < elementsCount + 1; ++i)
	{
		v.push_back("");
	}

	EXPECT_TRUE(v == String::Split(s, "&"));
}


TEST_F(StringTest, LTrimNothing)
{
	s = "haha   ";
	EXPECT_STREQ(s.c_str(), String::LTrim(s).c_str());
}

TEST_F(StringTest, LTrimNothingToTrim)
{
	s = "   haha   ";
	EXPECT_STREQ(s.c_str(), String::LTrim(s, "&").c_str());
}

TEST_F(StringTest, LTrimNormal)
{
	s = "&&&haha";
	EXPECT_STREQ("haha\0", String::LTrim(s, "&").c_str());
}

TEST_F(StringTest, LTrimIfToRemoveIsEmpty)
{
	s = "   haha";
	EXPECT_STREQ("   haha\0", String::LTrim(s, "").c_str());
}

TEST_F(StringTest, LTrimIfToRemoveIsZero)
{
	s = "   haha";
	EXPECT_STREQ("   haha\0", String::LTrim(s, "\0").c_str());
}

TEST_F(StringTest, RTrimNothing)
{
	s = "haha";
	EXPECT_STREQ(s.c_str(), String::RTrim(s).c_str());
}

TEST_F(StringTest, RTrimNothingToTrim)
{
	s = "   haha   ";
	EXPECT_STREQ(s.c_str(), String::RTrim(s, "&").c_str());
}

TEST_F(StringTest, RTrimNormal)
{
	s = "&&&haha&&&";
	EXPECT_STREQ("&&&haha\0", String::RTrim(s, "&").c_str());
}

TEST_F(StringTest, RTrimIfToRemoveIsEmpty)
{
	s = "haha   ";
	EXPECT_STREQ("haha   \0", String::RTrim(s, "").c_str());
}

TEST_F(StringTest, RTrimIfToRemoveIsZero)
{
	s = "haha   ";
	EXPECT_STREQ("haha   \0", String::RTrim(s, "\0").c_str());
}

TEST_F(StringTest, TrimNormal)
{
	s = "....haha..";
	EXPECT_STREQ("haha", String::Trim(s, ".").c_str());
}

TEST_F(StringTest, TrimNothingToTrim)
{
	s = "....haha..";
	EXPECT_STREQ(s.c_str(), String::Trim(s, "-").c_str());
}

TEST_F(StringTest, UppercaseString)
{
	s = "haha";
	EXPECT_STREQ("HAHA", String::Uppercase(s).c_str());
}

TEST_F(StringTest, UppercaseStringWithUnAlpha)
{
	s = "我haha00 AAA";
	EXPECT_STREQ("我HAHA00 AAA", String::Uppercase(s).c_str());
}

TEST_F(StringTest, UppercaseEmptyString)
{
	s.clear();
	EXPECT_STREQ("", String::Uppercase(s).c_str());
}

TEST_F(StringTest, LowercaseString)
{
	s = "HAHA";
	EXPECT_STREQ("haha", String::Lowercase(s).c_str());
}

TEST_F(StringTest, LowercaseStringWithUnAlpha)
{
	s = "我haha00 AAA";
	EXPECT_STREQ("我haha00 aaa", String::Lowercase(s).c_str());
}

TEST_F(StringTest, LowercaseEmptyString)
{
	s.clear();
	EXPECT_STREQ("", String::Lowercase(s).c_str());
}
